package com.skyable.device.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.skyable.device.entity.DeviceAuthLog;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author GaoYuan
 * @date 2019/11/5 9:42
 * @desc
 */
@Mapper
public interface DeviceAuthLogMapper extends BaseMapper<DeviceAuthLog> {


    /**
     * 创建设备授权日志表
     * @param domainId
     * @return
     */
    @Update({
            " CREATE TABLE IF NOT EXISTS ${domainId}_device_log ( " +
            " log_id bigint(20) NOT NULL AUTO_INCREMENT, " +
            " user_id bigint(20) DEFAULT NULL COMMENT '用户id', " +
            " sub_domain bigint(20) DEFAULT NULL COMMENT '子域id', " +
            " device_id varchar(20) DEFAULT NULL COMMENT '设备物理id', " +
            " log_type tinyint(4) DEFAULT NULL COMMENT '日志类型：1设备状态，2ota状态', " +
            " log_time datetime DEFAULT NULL COMMENT '日志记录时间', " +
            " details varchar(200) DEFAULT NULL COMMENT '设备日志详情', " +
            " success tinyint(4) DEFAULT NULL COMMENT '是否成功', " +
            " reserve varchar(20) DEFAULT NULL, " +
            " PRIMARY KEY (`log_id`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 "
    })
    int createDeviceAuthLogTable(@Param("domainId") Long domainId);

    /**
     * 批量写入授权日志表
     * @param deviceAuthLogList
     * @param domainId
     * @return
     */
    @Insert({
            "<script> " +
                    "INSERT INTO ${domainId}_device_log(user_id,sub_domain,device_id,log_type,log_time,details,success,reserve) VALUES " +
                    "<foreach collection='deviceAuthLogList' item='item' index='index' separator=','> " +
                    "(#{item.userId},#{item.subDomain},#{item.deviceId},#{item.logType},#{item.logTime},#{item.details},#{item.success},#{item.reserve}) " +
                    "</foreach>" +
                    "</script> "
    })
    int saveBatchDeviceAuthLog(@Param("deviceAuthLogList") List<DeviceAuthLog> deviceAuthLogList, @Param("domainId") Long domainId);
}
